package com.lxh.model;

import java.util.HashMap;
import java.util.Iterator;

public class PhoneNumber {
	
	public static final int PREFIX_CODE = 0;
	public static final int NUMBER = 1;
	
	private String oldFullNumber;
	private String prefixCode;
	private String number;
	
	private String newPrefixCode;
	private String newNumber;
	private boolean isSafe;
	
	public PhoneNumber(HashMap<String,String> prefixCodePairs, String phoneNumber) throws Exception{
		this.oldFullNumber = phoneNumber;
		phoneNumber = this.processInputPhoneNumber(phoneNumber);
		
		String[] phoneNumbers = this.splitPhoneNumber(prefixCodePairs, phoneNumber);
		
		if(phoneNumbers!=null && phoneNumbers.length==2){
			
			this.prefixCode = phoneNumbers[PREFIX_CODE];
			this.number = phoneNumbers[NUMBER];
			
			String[] newPhoneNumbers = this.initialNewPhoneNumber(prefixCodePairs, this.prefixCode, this.number);
			
			if(newPhoneNumbers!=null && newPhoneNumbers.length==2){
				
				this.newPrefixCode = newPhoneNumbers[PREFIX_CODE];
				this.newNumber = newPhoneNumbers[NUMBER];
				
			}else{
				
				throw new Exception("Not found New PrefixCode for this Phone Number: " + phoneNumber);
			}
			
		}else{
			
			throw new Exception("Not found PrefixCode for this Phone Number: " + phoneNumber);
		}
	}
	
	public PhoneNumber(HashMap<String,String> prefixCodePairs, String prefixCode, String number) throws Exception{
;
		this.prefixCode = prefixCode;
		this.number = number;
		
		String[] newPhoneNumbers = this.initialNewPhoneNumber(prefixCodePairs, this.prefixCode, this.number);
		
		if(newPhoneNumbers!=null && newPhoneNumbers.length==2){
			
			this.newPrefixCode = newPhoneNumbers[PREFIX_CODE];
			this.newNumber = newPhoneNumbers[NUMBER];
			
		}else{
			
			throw new Exception("Not found New PrefixCode for this Phone Number: " + prefixCode + number);
		}
	}

	public String getPrefixCode() {
		return prefixCode;
	}

	public String getNumber() {
		return number;
	}
	
	public String getNewPrefixCode() {
		return newPrefixCode;
	}

	public String getNewNumber() {
		return newNumber;
	}

	public boolean isSafe() {
		return isSafe;
	}
	
	public String getNewFullNumber(){
		return this.newPrefixCode + "-" + this.newNumber;
	} 

	public String getOldFullNumber() {
		return oldFullNumber;
	}

	//-------------- Business Function ---------------//
	public String[] splitPhoneNumber(HashMap<String,String> prefixCodePairs, String phoneNumber){
		
		String[] result = null;
		
		Iterator<?> oldPrefixCodes = prefixCodePairs.keySet().iterator();
		String oldprefixCode;
		String newPrefixCode;
		
		while(oldPrefixCodes.hasNext()){
			
			oldprefixCode = (String) oldPrefixCodes.next();
			
			//--------- Check Prefix Code in Phone Number --------//
			if(this.containPrefixCode(phoneNumber, oldprefixCode) == true){
				
				result = this.splitPhoneNumber(phoneNumber, oldprefixCode);
				
				newPrefixCode = prefixCodePairs.get(oldprefixCode);
				
				if(this.containPrefixCode(phoneNumber, newPrefixCode) == true){
				
					this.isSafe = false;
					
				}else{
					
					this.isSafe = true;
				}
			}
		}
		
		return result;
	}
	
	private String[] initialNewPhoneNumber(HashMap<String,String> prefixCodePairs, String oldPrefixCode, String oldNumber){
		String[] result = null;
		
		String newPrefixCode = prefixCodePairs.get(oldPrefixCode);
		
		if(newPrefixCode != null){
			result = new String[2];
			result[PREFIX_CODE] = newPrefixCode;
			result[NUMBER] = oldNumber;
		}
		
		return result;
	}
	
	private boolean containPrefixCode(String phoneNumber, String prefixCode){
		return phoneNumber.startsWith(prefixCode);
	}
	
	private String[] splitPhoneNumber(String phoneNumber, String prefixCode){
		String[] result = new String[2];
		
		result[PREFIX_CODE] = prefixCode;
		result[NUMBER] = phoneNumber.substring(prefixCode.length(), phoneNumber.length());
		
		return result;
	}
	
	private String processInputPhoneNumber(String phoneNumber){
		
		phoneNumber = phoneNumber.replace(" ", "");
        phoneNumber = phoneNumber.replace("-", "");
        phoneNumber = phoneNumber.replace("+", "");
        phoneNumber = phoneNumber.replace("*", "");
        phoneNumber = phoneNumber.replace("#", "");
        phoneNumber = phoneNumber.replace("(", "");
        phoneNumber = phoneNumber.replace(")", "");
        
        return phoneNumber;
	}
}
